package book;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import bookshopSM.BookshopGUI;
import bookshopSM.DrawPanel;
public class AddBook extends JPanel{
    private JButton addPictureButton,clearButton, writeButton;
    private BookshopGUI userInterface;
    String names1[] = {"图书编号", "书     名","分     类","定 价(元)","促销价(元)","作     者", "出 版 社"};
    String picturePath;
    static  Connection conn = null;
    private static String username ="root";
    private static String password="root" ;
    private static String driverClassName="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3307/books_system";

    private JPanel picturePanel;
    private DrawPanel myPanel;
    public AddBook(){
        JLabel btitle = new JLabel("添加新图书            ");
        btitle.setFont(new   java.awt.Font("楷体", 1, 20));
        myPanel=new DrawPanel();
        Box box1 = Box.createHorizontalBox();
        Box boxinner=Box.createVerticalBox();
        addPictureButton=new JButton("浏览图书封面图片   ");
        userInterface = new BookshopGUI( names1 );
        //myPanel.paintComponent(null);
        myPanel.draw();
        boxinner.add(myPanel);
        boxinner.add(addPictureButton);

        addPictureButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                JFileChooser dlg = new JFileChooser();
                FileNameExtensionFilter F1 = new FileNameExtensionFilter("图片文件","jpg","png");
                dlg.setFileFilter(F1);
                dlg.setDialogTitle("Open JPG file");
                int result = fi1(dlg);
                //int result = dlg.showSaveDialog(this);  // 打"开保存文件"对话框
                if (result == JFileChooser.APPROVE_OPTION) {
                    picturePath=dlg.getSelectedFile().getPath();
                    myPanel.setimg(picturePath);
                    myPanel.draw();
                }
            }

        });
        Box box2=Box.createVerticalBox();
        box1.add(userInterface);
        box1.add(boxinner);
        box2.add(Box.createVerticalStrut(15));
        box2.add(btitle);
        box2.add(Box.createVerticalStrut(15));
        box2.add(box1);
        this.add( box2);
        writeButton =userInterface.getDoTask1Button();
        writeButton.setText( "确认" );
        writeButton.addActionListener(
                new ActionListener() {
                    public void actionPerformed( ActionEvent event )
                    {
                        String fieldValues[] = userInterface.getFieldValues();
                        try {
                            conn = DriverManager.getConnection(url,username,password);
                            Statement stmt=conn.createStatement();
                            stmt.executeUpdate("INSERT INTO Book(Bname,Bsort,Bprice,Bsellprice,Bauthor,Bpress,Bcover) " +
                                    "VALUES ('"+fieldValues[1]+"','"+
                                    fieldValues[2]+"',"+
                                    Float.valueOf(fieldValues[3])+","+
                                    Float.valueOf(fieldValues[4])+",'"+
                                    fieldValues[5]+"','"+
                                    fieldValues[6]+"','"+
                                    picturePath+
                                    "')");
                            ResultSet rs = stmt.executeQuery("select top 1 Bno from Book order by Bno desc");
                            rs.next();//rs指向第一行之前
                            int newvno=rs.getInt("Bno");
                            userInterface.fields[0].setText(String.valueOf(newvno));
                            JOptionPane.showMessageDialog(null, "添加成功");
                        }
                        catch(SQLException e1){
                            System.out.println(e1);
                        }
                        catch (Exception ee){
                            System.out.println(ee);
                        }
                        finally{
                            if(conn!=null){
                                try {
                                    conn.close();
                                } catch (SQLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                                conn=null;
                            }
                        }
                    }
                }


        );
        clearButton = userInterface.getDoTask2Button();
        clearButton.setText( "清除" );
        clearButton.addActionListener(
                new ActionListener() {
                    public void actionPerformed( ActionEvent event )
                    {
                        userInterface.clearFields();
                    }
                }
        );
    }

    public int fi1(JFileChooser dlg)
    {
        int result = dlg.showOpenDialog(this);  // 打开"打开文件"对话框
        return result;
    }

}
